//
//  WelcomeView.swift
//  MXWeiBo
//
//  Created by Apple on 16/2/26.
//  Copyright © 2016年 Applea. All rights reserved.
//

import UIKit
import SnapKit
//欢迎新特性
class WelcomeView: UIView {
    override init(frame: CGRect) {
        super.init(frame: UIScreen.mainScreen().bounds)
      setuoUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    deinit {
    print("\(classForCoder)\(__FUNCTION__)")
    }
    
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        removeFromSuperview()
    }
    
//  MARK:--生命周期
//    添加到父视图
    override func didMoveToSuperview() {
        super.didMoveToSuperview()
    }
//    添加到窗口 － 已经显示
    override func didMoveToWindow() {
        super.didMoveToWindow()
        
//        更新约束
        avatarView.snp_updateConstraints { (make) -> Void in
            make.centerY.equalTo(self).offset(-100)
        }
        
//        动画
        UIView.animateWithDuration(1.2,
            delay: 0.0,
            usingSpringWithDamping: 0.8,
            initialSpringVelocity: 10,
            options: [],
            animations: { () -> Void in
            self.layoutIfNeeded()
            }) { (_) -> Void in
                self.removeFromSuperview()
        }
    }
    override func layoutSubviews() {
        super.layoutSubviews()
    }
//  MARK:-- 私有控件
//    头像视图
    private lazy var avatarView = UIImageView(mx_imageName: "avatar_default_big")
//欢迎标签
    private lazy var welcomeLabel = UILabel(text: "欢迎回来", fontsize: 18)
}
//    MARK:  --设置界面
extension WelcomeView {
    private func setuoUI() {
//    1.设置背景图片
        backgroundColor = UIColor(patternImage: UIImage(named: "ad_background")!)
//    2.添加控件
        addSubview(avatarView)
        addSubview(welcomeLabel)
//        半径
        let iconWH: CGFloat = 45
        avatarView.layer.cornerRadius = iconWH
        avatarView.layer.masksToBounds = true
        
        
//        3.自动布局
        avatarView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(100)
            make.size.equalTo(CGSize(width: iconWH * 2, height: iconWH * 2))
        }
        welcomeLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(avatarView)
            make.top.equalTo(avatarView.snp_bottom).offset(20)
        }
//        4.强行更新约束
        layoutIfNeeded()
//        5.设置头像
        avatarView.mx_setImageWithUrl(UserAccount.sharedUserAccount.avatar_large, placeholdName: "avatar_default_big")
    }
}